import http from '@ohos.net.http';
import { ResponseResult } from '../pages/entity/BaseReponse';
import { CommonConstant as Const, ContentType } from '../pages/entity/CommonConstant';
import PrefUtil from './PrefUtil';


export function httpRequestGet(url: string, parameters?: object): Promise<ResponseResult> {
  let httpRequest = http.createHttp();
  //发起请求
  let responseResult = httpRequest.request(url, {
    method: http.RequestMethod.GET,
    readTimeout: Const.HTTP_READ_TIMEOUT,
    header: {
      'Content-Type': ContentType.JSON
    },
    connectTimeout: Const.HTTP_READ_TIMEOUT,
    extraData: parameters ? parameters : {}
  });

  let serverData: ResponseResult = new ResponseResult();

  //返回结果处理
  return responseResult.then((value: http.HttpResponse) => {
    //接口请求成功
    if (value.responseCode === Const.HTTP_CODE_200) {
      let result = `${value.result.toString()}`;
      //将JSON解析成对象
      let resultJson: ResponseResult = JSON.parse(result);
      if (resultJson.code === Const.SERVER_CODE_SUCCESS) {
        serverData.data = resultJson.data;
      }
      serverData.code = resultJson.code;
      serverData.message = resultJson.message;
    }
    //接口请求失败
    else {
      serverData.message = `${'Http请求失败'}&${value.responseCode}`;
    }
    return serverData;
  }).catch(() => {
    serverData.message = 'Http请求失败';
    return serverData;
  })
}

/**
 *  * Get请求
 *  * @param url
 *  * @returns
 *  */
export async function httpRequestGetWithToken(url: string, parameters?: object): Promise<ResponseResult> {
  let token = await PrefUtil.getString("token")
  let httpRequest = http.createHttp();
  //发起请求
  let responseResult = httpRequest.request(url, {
    method: http.RequestMethod.GET,
    readTimeout: Const.HTTP_READ_TIMEOUT,
    header: {
      'Content-Type': ContentType.JSON,
      'token': token
    },
    connectTimeout: Const.HTTP_READ_TIMEOUT,
    extraData: parameters? parameters : {}
  });

  let serverData: ResponseResult = new ResponseResult();

  //返回结果处理
  return responseResult.then((value: http.HttpResponse) => {
    //接口请求成功
    if (value.responseCode === Const.HTTP_CODE_200) {
      let result = `${value.result.toString()}`;
      //将JSON解析成对象
      let resultJson: ResponseResult = JSON.parse(result);
      if (resultJson.code === Const.SERVER_CODE_SUCCESS) {
        serverData.data = resultJson.data;
      }
      serverData.code = resultJson.code;
      serverData.message = resultJson.message;
    }
    //接口请求失败
    else {
      serverData.message = `${'Http请求失败'}&${value.responseCode}`;
    }
    return serverData;
  }).catch(() => {
    serverData.message = 'Http请求失败';
    return serverData;
  })
}

export function httpRequestPost(url:string, parameters: object): Promise<ResponseResult> {
  let httpRequest = http.createHttp();
  //发起请求
  let responseResult = httpRequest.request(url, {
    method: http.RequestMethod.POST,
    readTimeout: Const.HTTP_READ_TIMEOUT,
    header: {
      'Content-Type': ContentType.JSON
    },
    connectTimeout: Const.HTTP_READ_TIMEOUT,
    extraData: parameters
  });

  let serverData: ResponseResult = new ResponseResult();
  return responseResult.then((value: http.HttpResponse) => {
    //接口请求成功
    if (value.responseCode === Const.HTTP_CODE_200) {
      let result = `${value.result.toString()}`;
      //将JSON解析成对象
      let resultJson: ResponseResult = JSON.parse(result);
      if (resultJson.code === Const.SERVER_CODE_SUCCESS) {
        serverData.data = resultJson.data;
      }
      serverData.code = resultJson.code;
      serverData.message = resultJson.message;
    }
    //接口请求失败
    else {
      serverData.message = `${'Http请求失败'}&${value.responseCode}`;
    }
    return serverData;
  }).catch(() => {
    serverData.message = 'Http请求失败';
    return serverData;
  })
}

export async function httpRequestPostWithToken(url:string, parameters?: object): Promise<ResponseResult> {
  let token = await PrefUtil.getString("token")
  let httpRequest = http.createHttp();
  //发起请求
  let responseResult = httpRequest.request(url, {
    method: http.RequestMethod.POST,
    readTimeout: Const.HTTP_READ_TIMEOUT,
    header: {
      'Content-Type': ContentType.JSON,
      'token': token
    },
    connectTimeout: Const.HTTP_READ_TIMEOUT,
    extraData: parameters
  });

  let serverData: ResponseResult = new ResponseResult();
  return responseResult.then((value: http.HttpResponse) => {
    //接口请求成功
    if (value.responseCode === Const.HTTP_CODE_200) {
      let result = `${value.result.toString()}`;
      //将JSON解析成对象
      let resultJson: ResponseResult = JSON.parse(result);
      if (resultJson.code === Const.SERVER_CODE_SUCCESS) {
        serverData.data = resultJson.data;
      }
      serverData.code = resultJson.code;
      serverData.message = resultJson.message;
    }
    //接口请求失败
    else {
      serverData.message = `${'Http请求失败'}&${value.responseCode}`;
    }
    return serverData;
  }).catch(() => {
    serverData.message = 'Http请求失败';
    return serverData;
  })
}

